package com.gator.mybatis.sqlsesson;


import com.gator.mybatis.configuration.Configuration;
import com.gator.mybatis.executor.Executor;

/**
 * @Authror PABLO
 * @Date 2022/4/26 21:29
 * @Desc
 */
public class DefaultSqlSession implements SqlSession {


    public DefaultSqlSession(Configuration configuration, Executor executor) {
        this.configuration = configuration;
        this.executor = executor;
    }

    private Configuration configuration;

    private Executor executor;


    /**
     * @Description: 动态代理获取接口映射器实例对象
     * @Author: PABLO
     * @Date: 2022/4/26 22:57
     * @Params: [clazz]
     * @Return: T
     **/
    @Override
    public <T> T getMapper(Class<T> type) {

        return configuration.<T>getMapper(type, this);
    }

    /**
     * @Description: 代理类调用，执行invoke
     * @Author: PABLO
     * @Date: 2022/4/26 22:59
     * @Params: 
     * @Return: 
     **/
    @Override
    public <T> T selectOne(String statementId, Object param) {
        //获取SQL语句
        String sql = configuration.sqlMappings.getString(statementId);
        return (T) executor.query(sql, param);
    }

}
